import os
import dashscope
from dashscope import Generation

# messages = [
#         {'role': 'system', 'content': '你是一个助手，根据前文回答最新的问题.'},
#     ]
#
# def get_message(msg):
#     # 单轮对话
#     single_message = {'role': 'user', 'content': msg}
#     messages.append(single_message)
#     response = Generation.call(
#         # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
#         api_key=os.getenv('DASHSCOPE_API_KEY'),
#         model="qwen-plus",  # 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
#         messages=messages,
#         result_format='message'
#     )
#     # print(response)
#     system_output = response.output.choices[0].message.content
#     messages.append({'role': 'system', 'content': system_output})
#     if response.status_code == 200:
#         print(response.output.choices[0].message.content)
#         return response.output.choices[0].message.content
#     else:
#         print(f"HTTP返回码：{response.status_code}")
#         print(f"错误码：{response.code}")
#         print(f"错误信息：{response.message}")
#         print("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
#         return f"错误码：{response.code}，错误信息：{response.message}"

# 多轮对话
messages = [
    {
        "role": "system",
        "content": "你是一个全能的助手",
    }
]

def get_response(messages):
    response = Generation.call(
        # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        model="qwen-plus",
        messages=messages,
        result_format="message",
    )
    return response

def get_message(message):
    # assistant_output = "全能的助手随时回答您的问题"
    # print(f"模型输出：{assistant_output}\n")
    #
    # user_input = input("请输入：")
    # 将用户问题信息添加到messages列表中
    messages.append({"role": "user", "content": message})
    assistant_output = get_response(messages).output.choices[0].message.content
    # 将大模型的回复信息添加到messages列表中
    messages.append({"role": "assistant", "content": assistant_output})
    print(f"模型输出：{assistant_output}")
    print("\n")
    return assistant_output
